package http

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
	"github.com/argoproj/argo-workflows/v3/util/logging"
)

func TestCreateOauth2Client(t *testing.T) {
	endpointParams := []wfv1.OAuth2EndpointParam{{Key: "key", Value: "value"}}
	scopes := []string{"some", "scopes"}
	client := CreateOauth2Client(logging.TestContext(t.Context()), "clientID", "clientSecret", "tokenURL", scopes, endpointParams)

	assert.NotNil(t, client)
}

func TestCreateClientWithCertificateInvalidCert(t *testing.T) {
	client, err := CreateClientWithCertificate([]byte("invalidCert"), []byte("invalidKey"))

	require.Error(t, err)
	assert.Nil(t, client)
}

func TestCreateClientWithCertificateValidCert(t *testing.T) {
	client, err := CreateClientWithCertificate([]byte(CertPem), []byte(KeyPem))

	require.NoError(t, err)
	assert.NotNil(t, client)
}

// test certificate pair
const (
	CertPem = `-----BEGIN CERTIFICATE-----
MIIEmjCCAoICCQDQejieQSZTxzANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0
ZXN0MB4XDTIyMDQyNTEzNDc0MloXDTMyMDQyMjEzNDc0MlowDzENMAsGA1UEAwwE
dGVzdDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAMHT/tfskuXizar1
5DDrSkaT1cuCdQhEO7b6haxfvfMJPY9sxaxR570bw5TWQzA0xdAeUzSCbRsvxw6b
fEyLD4NajdXtcKocYUUcLclzjgyogTDPqlzAfDVZD25ySOTZ150pQaBuIi6TgnqH
WdJEh9w5//5VZmKyMx49JZMW7ADb9qYxkKVPIan3aNEXOO4SxyjsSekUFefkZOld
/RVZ8nO8hnDQ7r5NXsIIWVh35A94CA8y6QpKL2qiEFW1fofRcr/Fe/Y/5ohBQ1Ur
NMcX87zm9kXX1y6wbp3wn5f1PUa1sCUPlxChmRmPPmr4yIqq0a8C1d71jOIbhkox
7A30HsP1D3rdxU6eb7KBYb7kShZge1batHRogRe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